package com.enotourister.core;

import com.enotourister.core.model.Contract;
import com.enotourister.core.model.Tourist;
import com.enotourister.core.param.ParameterType;
import com.enotourister.core.param.Parameter;
import com.enotourister.core.param.ServiceTypeEnum;
import com.enotourister.core.pool.Pool;
import com.enotourister.core.proposal.Proposal;
import com.enotourister.core.proposal.ProposalListener;
import com.enotourister.core.rfp.RequestForProposal;
import com.enotourister.core.rfp.RequestForProposalManager;

public class Main implements ProposalListener {
	private static final long serialVersionUID = 4170157673539655742L;
	private static final int MIN_NUMBER_OF_PROPOSALS = 4;
	
	private RequestForProposalManager rfpMgr = null;
	private Pool pool = null;
	private int numberOfProposals = 0;
	private int randomProposalIndex = 0;

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		double maxMoney = 200; // This parameter comes from GUI
		String serviceType = ServiceTypeEnum.MIX.getParam(); // This parameter comes from GUI
		Tourist tourist = new Tourist("test@test.com", "Pedro Pérez"); // This parameter comes from GUI
		
		// Setting randomProposalIndex
		randomProposalIndex = (int) Math.round(Math.random() * (MIN_NUMBER_OF_PROPOSALS - 1));
		System.out.println("Main: randomProposalIndex " + randomProposalIndex);

		// Subscribing in pool 
		// Is implemented here for knowing when to pick a proposal (based on: randomProposalIndex)
		pool = Pool.getInstance();
		pool.subscribe(this);  

		/***** Preparing RFP *****/
		
		// Preparing params
		Parameter params = new Parameter();
		params.put(ParameterType.SERVICE_TYPE, serviceType);
		params.put(ParameterType.MAX_MONEY, Double.toString(maxMoney));

		// Requesting the proposal
		rfpMgr = new RequestForProposalManager(params, tourist);
		rfpMgr.requestForProposals();

	}
		
	@Override
	public void submitProposal(Proposal proposal) { // Every new proposal can be updated in a table
		System.out.println("Main: New proposal[" + numberOfProposals + "] " + proposal);

		if (numberOfProposals == randomProposalIndex) {
			Contract contract = proposal.contract();
			
			if (contract != null) 
				System.out.println("Main: Proposal contracted: " + contract);
			else
				System.out.println("Main: Proposal not contracted: " + proposal);
		}

		numberOfProposals++;
	}

	@Override
	public RequestForProposal getRequestForProposal() {
		return rfpMgr.getRequestForProposal();
	}	
}

